import pytest
from selenium.common.exceptions import TimeoutException


def test_valid_login(user, login_page):
    """
    Test checks authorization with valid user.
    """
    profile_page = login_page.input_login(user.login) \
        .input_password(user.password)

    assert profile_page.get_title() == "Яндекс Id"
    assert profile_page.cookie_is_set("Session_id")


def test_login_with_comeback(user, login_page):
    """
    Test checks back button from authorization form.
    """
    profile_page = login_page.input_login(user.login) \
        .back_by_user_bt_click() \
        .input_login(user.login) \
        .input_password(user.password)

    assert profile_page.cookie_is_set("Session_id")


def test_logout(user, login_page):
    """
    Test checks logout function from profile page.
    """
    authorization_page = login_page.input_login(user.login) \
        .input_password(user.password) \
        .logout()

    assert authorization_page.get_title() == "Авторизация"
    assert not authorization_page.cookie_is_set("Session_id")


def test_logout_by_account_list(user, login_page):
    """
    Test checks logout function from profile page.
    """
    authentication_page = login_page.input_login(user.login) \
        .input_password(user.password) \
        .back_to_account_list() \
        .logout_with_list_flushing()

    assert authentication_page.get_title() == "Авторизация"
    assert not authentication_page.cookie_is_set("Session_id")


def test_empty_user_data(login_page):
    """
    Test checks login function with empty data.
    """
    with pytest.raises(TimeoutException):
        login_page.input_login("")
    assert login_page.get_error_text() == "Логин не указан"


def test_login_with_fake_user(non_existing_user, login_page):
    """
    Test checks login function with invalid login.
    """
    with pytest.raises(TimeoutException):
        login_page.input_login(non_existing_user.login)
    assert login_page.get_error_text() == "Такого аккаунта нет"
